<template>
  <root class="shop-root">
    <wux-spin nested :spinning="spinning">
      <wux-cell-group title="当前酒店">
        <wux-cell :title="shop.title === undefined ? '暂无' : shop.title"></wux-cell>
      </wux-cell-group>
      <wux-cell-group title="请选择酒店">
        <wux-spin nested :spinning="contentSpinning">
          <wux-cell
            v-for="(item, index) in shopList"
            :key="index"
            :title="item.title"
            @click="selectShop(item)"
            :extra="item.distance"
          ></wux-cell>
        </wux-spin>
      </wux-cell-group>
    </wux-spin>
  </root>
</template>

<script>
import commodityApi from "@/api/commodity";

import { getDistance } from "@/utils/index";
export default {
  data() {
    return {
      shopList: [],
      spinning: false,
    };
  },
  computed: {
    shop() {
      return this.$store.state.shop;
    },
  },
  mounted() {
    wx.showShareMenu();
    const self = this;
    wx.getLocation({
      type: "wgs84",
      success(res) {
        const latitude = res.latitude;
        const longitude = res.longitude;
        self.$store.dispatch("saveLocate", latitude + "," + longitude);
        self.queryShop();
      },
    });
  },
  methods: {
    queryShop() {
      this.spinning = true;
      const self = this;
      this.$http
        .post({
          url: commodityApi.queryShopList,
          method: "POST",
        })
        .then((res) => {
          self.spinning = false;
          res.list.forEach((item) => {
            item.distance = getDistance(
              self.$store.state.latitude,
              self.$store.state.longitude,
              item.locate.split(",")[0],
              item.locate.split(",")[1]
            );
          });
          res.list.sort(function (a, b) {
            return parseInt(a.distance) > parseInt(b.distance) ? 1 : -1;
          });
          self.shopList = res.list;
        })
        .then((res) => {
          self.spinning = false;
        });
    },
    selectShop(item) {
      this.$store.dispatch("saveShop", item);
      this.$router.push({ path: "/pages/index", reLaunch: true });
    },
  },
};
</script>